<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="cancel">
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-width="120px"
      style="width:75%;margin:0 auto"
    >
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="mobile" label="手机号">
        <el-input v-model="form.mobile" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="请选择入职时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" placeholder="请选择活动区域">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input
          @focus="comTree"
          @blur="treeBlur"
          v-model="form.departmentName"
          autocomplete="off"
        ></el-input>
        <el-tree
          v-if="showTree"
          default-expand-all
          :data="treeData"
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="转正时间"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
//
import { getDepartments } from "@/api/departments";

import { transToTreeData } from "@/utils";
// 导入枚举类
import eumable from "@/api/constant/employees";

// 导入新增员工
import { addEmp } from "@/api/employees";
export default {
  name: "addEmployee",
  props: {
    showDialog: {
      require: true,
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      hireType: eumable.hireType,
      rules: {
        username: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" }
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" }
        ],
        workNumber: [
          { required: true, message: "请输入工号", trigger: "blur" }
        ],
        departmentName: [
          { required: true, message: "请输入组织名称", trigger: "change" }
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "blur" }
        ]
      },
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workName: "",
        departmentName: "",
        correctionTime: ""
      },
      showTree: false,
      treeData: [],
      defaultProps: {
        label: "name",
        children: "children"
      }
    };
  },
  methods: {
    handleNodeClick(data, node, tree) {
      this.form.departmentName = data.name;
      this.showTree = false;
    },
    async comTree() {
      this.showTree = true;
      let res = await getDepartments();
      let list = res.data.depts;
      this.treeData = transToTreeData(list, "");
    },
    treeBlur(e) {
      if (e.relatedTarget.className.indexOf("el-tree-node") == -1) {
        this.showTree = false;
      }
    },
    async save() {
      this.$refs.form.validate(async v => {
        if (v) {
          await addEmp(this.form);
          this.$message.success("添加成功！！");
          let pageInfo = this.$parent.pageInfo;
          let lastPage = Math.ceil(++pageInfo.total / pageInfo.size);
          pageInfo.page = lastPage;
          console.log(pageInfo);
          this.$parent.getEmployeeList();
          this.$emit("update:showDialog", false);
        }
      });
    },
    cancel() {
      this.form = {};
      this.$refs.form.resetFields();
      this.$emit("update:showDialog", false);
    }
  }
};
</script>

<style></style>
